<?php
/**
 *  fabchannel Plugin  
 *
 *  @license    GPL 2 ( http://www.gnu.org/licenses/gpl.html )
 *  @author     jaloma.ac [at] googlemail [dot] com
 *
 *  Description:
 *  Plugin to display fabchannel-Live-Concerts
 *
 */
 
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
 
/**
 *  fabchannel Plugin 
 */
class syntax_plugin_fabchannel extends DokuWiki_Syntax_Plugin {

  var $dflt = array(
  'width' => 640,
  'height' => 360,
  'allowFullScreen'=> 'true',
  'artist' => 'off',
  'channel' => 'off',
  'concertoftheday' => 'off',
  'newinarchive' => 'off'
);
 
  /**
   *  Plugin Info 
   */
  function getInfo( ) {
    return array(
      'author' => 'Jürgen A. Lamers',
      'email'  => 'jaloma.ac [at] googlemail [dot] com',
      'date'   => '2008-11-01',
      'name'   => 'fabchannel',
      'desc'   => 'fabchannel Plugin <fabchannel artist="" width="" height=""/>',
      'url'    => 'http://www.dokuwiki.org/plugin:fabchannel',
    );
  }
 
  /**
   *  Typology?
   */  
  function getType( ) { 
		return 'substition'; 
	}
 
  /**
   *  Sort Code? 
   */  
  function getSort( ) {
    return 316;
  }
 
  /**
   *  Pattern Matching? 
   */  
  function connectTo($mode) {       
    $this->Lexer->addSpecialPattern( '<fabchannel.*?/>', $mode, 'plugin_fabchannel' ); 
  }
 
 function matchLength() {
 	return strlen("<fabchannel");
 }
  /**
   *  Parsing
   *  1. Very rough parsing involved
   *  2. Applet parameters not included
   */    
  function handle( $match, $state, $pos, &$handler ) {
 //   list($str_params) = explode('>',substr($match,8,-2),-1);
    $match = html_entity_decode(substr($match,$this->matchLength(),-2));
    $gmap = $this->_extract_params($match);
    return $gmap;
  }
  /**
   * extract parameters for the googlemap from the parameter string
   *
   * @param   string    $str_params   string of key="value" pairs
   * @return  array                   associative array of parameters key=>value
   */
  function _extract_params($str_params) {
    $param = array();
    preg_match_all('/(\w*)="(.*?)"/us',$str_params,$param,PREG_SET_ORDER);
    if (sizeof($param) == 0) {
      preg_match_all("/(\w*)='(.*?)'/us",$str_params,$param,PREG_SET_ORDER);
    }
    // parse match for instructions, break into key value pairs      
    $gmap = $this->dflt;
    foreach($param as $kvpair) {
      list($match,$key,$val) = $kvpair;
//      $key = strtolower($key);
      if (isset($gmap[$key])) $gmap[$key] = $val;        
    }

    return $gmap;
  }
 
  /**
   *  Rendering
   */  
  function render( $mode, &$renderer, $data ) {
    if($mode == 'xhtml'){
    	global $conf;
/*
<embed src="http://www.fabchannel.com/embed/player.swf?ap=artist.polarkreis_18" 
type="application/x-shockwave-flash"	width="400" height="350" allowFullScreen="true"></embed>
*/
	  $width = $data['width'];
	  $height = $data['height'];
	  $artist = $data['artist'];
	  $channel = $data['channel'];
	  $concertoftheday = $data['concertoftheday'];
	  $newinarchive = $data['newinarchive'];
	  $allowFullScreen=$data['allowFullScreen'];
	  
	  $archive = "http://www.fabchannel.com/embed/player.swf";
	  $txt = "";
	  $txt .= '<embed src="'.$archive.'?ap=';
	  if ($concertoftheday != 'off') {
	  	$txt .= 'fabchannel.concertoftheday.a';
 	  } else if ($newinarchive != 'off') {
		$txt .= 'fabchannel.newinarchive.a';
	  } else if ($artist != 'off') {
  		$txt .= 'artist.'.$artist;
	  } else if ($channel != 'off') {
	  	$txt .= 'fabchannel.'.$channel;
	  }
	  $txt .= '" ';
	  $txt .= ' type="application/x-shockwave-flash" ';
	  $txt .= ' width="'.$width.'" ';
	  $txt .= ' height="'.$height.'" ';
	  $txt .= ' allowFullScreen="'.$allowFullScreen.'" ';
	  $txt .= '>';
	  $txt .= '</embed>';
	  $renderer->doc .= $txt;
      return true;
    } else {
      return false;
    }
  }   
}// class
